package com.zbbt.sportslottery.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.HuoDongQuanData;
import com.zbbt.sportslottery.util.TimeUtil;

public class HDQListAdapter extends BaseAdapter {
	private OnDialogClickListener clickListener;
	private OnBottomClickListener clickLonbottomistener;
	private LayoutInflater mInflater;
	private List<HuoDongQuanData> dataList;
	private Context context;

	public HDQListAdapter(Context context, List<HuoDongQuanData> dataList) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final String code;
		final StringBuffer all_str;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_huodong, null);
			holder = new ViewHolder();
			holder.huodongquan_name_tx = (TextView) convertView.findViewById(R.id.huodongquan_name_tx);
			holder.value_tx = (TextView) convertView.findViewById(R.id.value_tx);
			holder.validity_tx = (TextView) convertView.findViewById(R.id.validity_tx);
			holder.ifuse_btn = (TextView) convertView.findViewById(R.id.ifuse_btn);
			holder.yuan_tx = (TextView) convertView.findViewById(R.id.yuan_tx);
			holder.money_text = (TextView) convertView.findViewById(R.id.money_text);
			holder.huoLayout=(LinearLayout) convertView.findViewById(R.id.huodong_pic);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		HuoDongQuanData mBean = dataList.get(position);
		int status = mBean.getStatus();
		if (status == 1) {// 未使用
			holder.validity_tx.setText("有效期至:" + TimeUtil.getTimeDate(mBean.getEndEffectiveTime()));
		} else if (status == 2) {// 已使用
			holder.validity_tx.setText("有效期至:" + TimeUtil.getTimeDate(mBean.getEndEffectiveTime()));
		} else if (status == 3) {// 已过期
			holder.validity_tx.setText("有效期至:" + TimeUtil.getTimeDate(mBean.getEndEffectiveTime()));
		}
		holder.value_tx.setText(mBean.getPrice()+"");
		holder.huodongquan_name_tx.setText(mBean.getCouponName());
		if (mBean.getStatus() != 0) {
			if (mBean.getStatus() == 1) {
				holder.ifuse_btn.setText("未使用>");
				holder.huoLayout.setBackgroundResource(R.drawable.huodongquan_pic);
				holder.ifuse_btn.setTextColor(context.getResources().getColor(R.color.white));
				holder.ifuse_btn.setBackgroundResource(R.drawable.btn_blue);
				holder.ifuse_btn.setPadding(24, 0, 24, 0);
			} else if (mBean.getStatus() == 2) {
				holder.huoLayout.setBackgroundResource(R.drawable.huodongquan_isuse_bg);
				holder.ifuse_btn.setText("已使用>");
				holder.ifuse_btn.setTextColor(context.getResources().getColor(R.color.gray));
				holder.ifuse_btn.setBackgroundResource(R.drawable.btn_gray);
				holder.ifuse_btn.setPadding(24, 0, 24, 0);
				holder.yuan_tx.setTextColor(context.getResources().getColor(R.color.gray_tq));
				holder.value_tx.setTextColor(context.getResources().getColor(R.color.gray_tq));
				holder.money_text.setTextColor(context.getResources().getColor(R.color.gray_tq));
			} else if (mBean.getStatus() == 3) {
				holder.huoLayout.setBackgroundResource(R.drawable.huodongquan_isuse_bg);
				holder.ifuse_btn.setText("已过期>");
				holder.ifuse_btn.setTextColor(context.getResources().getColor(R.color.gray));
				holder.ifuse_btn.setBackgroundResource(R.drawable.btn_gray);
				holder.ifuse_btn.setPadding(24, 0, 24, 0);
				holder.yuan_tx.setTextColor(context.getResources().getColor(R.color.gray_tq));
				holder.value_tx.setTextColor(context.getResources().getColor(R.color.gray_tq));
				holder.money_text.setTextColor(context.getResources().getColor(R.color.gray_tq));
			}
		}

		holder.ifuse_btn.setFocusable(false);
		holder.ifuse_btn.setFocusableInTouchMode(false);
		code = mBean.getCouponNum();
		all_str = new StringBuffer();
		all_str.append(mBean.getCouponNum());
		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (clickListener != null) {
					clickListener.onButtonClick(code, all_str.toString());
				}
			}
		});
		if (position == dataList.size() - 1) {
			if (clickLonbottomistener != null) {
				clickLonbottomistener.onBottomClick();
			}
		}
		return convertView;
	}

	static class ViewHolder {
		public TextView huodongquan_name_tx;
		public TextView value_tx;
		public TextView validity_tx;
		public TextView ifuse_btn;
		public TextView yuan_tx;
		public TextView money_text;
		public LinearLayout huoLayout;
	}

	public interface OnBottomClickListener {
		public void onBottomClick();
	}

	public void setOnBottomClickListener(OnBottomClickListener clickListener) {
		this.clickLonbottomistener = clickListener;
	}

	public interface OnDialogClickListener {
		public void onButtonClick(String code, String all_str);
	}

	public void setOnDialogClickListener(OnDialogClickListener clickListener) {
		this.clickListener = clickListener;
	}
}
